% Chakram is a subproject of Hexena - Haskell Executable Analyser
% License:			GPL-3
% Maintainer:		Montez Fitzpatrick
% Stability:		Experimental
% Portability:		GHC
% Module:			Hexena.Codec.Parser.DOSStub

DOSStub Parser

\begin{code}
module Hexena.Codec.Parser.DOSStub (parseDosStub) where

import qualified Data.ByteString.Lazy as L
import Data.Binary.Get

import Data.Word
import Data.Int (Int64)

import System.IO (openFile, IOMode(ReadMode), hFileSize, hClose)

import Hexena.Data.DOSStub (DOSStub(..))

-- | Assumes continuation in a stream.
parseDosStub' :: Monad m => Int64 -> L.ByteString -> m (DOSStub, L.ByteString)
parseDosStub' szOfDosSb fh = do
	let (dStb,rest) = L.splitAt szOfDosSb fh
	return (DOSStub dStb, rest)
	
-- | slices out the DOS Stub program as a bytestring.  It expects to be at the current
--   offset of the start of the DOS Stub and asks for an argument which represents
--   the total size of the DOS Stub.  This is obviously not safe.
parseDosStub :: Int64 -> Get DOSStub
parseDosStub szOfDosSb = do
	dStb <- getLazyByteString szOfDosSb
	return $ DOSStub dStb
\end{code}
\ignore {
\begin{code}
-- | grabs and returns the DOS Stub as bytes.  Assumes MzHeader
--   has been consumed and start
getDosStub stubLen = undefined

-- | skips the DOS Stub in stream and returns rest of 
--   bytestream
dropDosStub = undefined
-- | Will lazily append DOS Stub to bytestream, returning new
--   Bytestream
appendDosStub = undefined
-- | Will replace the current DOS Stub with a different specified one,
--   operation is relatively expensive.
replaceDosStub = undefined

-- | Internal Function. size of the DOS Stub file on disk
sizeOfFile :: FilePath -> IO Integer
sizeOfFile dosStubFile = do
	handle <- openFile dosStubFile ReadMode
	fsize <- hFileSize handle
	hClose handle
	return fsize
\end{code}
}
